package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.UserModel;
import com.zeng.jiudian.dao.UserDao;
import com.zeng.jiudian.dao.impl.UserDaoImpl;
import com.zeng.jiudian.service.UserService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/16-15:52
 */
public class UserServiceImpl implements UserService
{
    @Override
    public int addUser(UserModel userModel)
    {
        UserDao userDao = new UserDaoImpl();
        int affect = 0;
        if (findByUsername(userModel.getUsername()))
        {
            affect = -1;
        }
        else
        {
            affect = userDao.addUser(userModel);
        }
        if (affect == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return affect;
    }

    @Override
    public boolean findByUsername(String username)
    {
        UserDao userDao = new UserDaoImpl();

        return userDao.findByUsername(username);
    }

    @Override
    public int login(UserModel userModel)
    {
        int result = 0;
        if (userModel.getCode().equalsIgnoreCase(userModel.getSessionCode()))
        {
            if (findByUsernameAndPassword(userModel))
            {
                result = 1;
            }
        }
        else
        {
            result = -1;
        }

        return result;
    }

    @Override
    public boolean findByUsernameAndPassword(UserModel userModel)
    {
        UserDao userDao = new UserDaoImpl();

        return userDao.findByUsernameAndPassword(userModel);
    }

    @Override
    public List<UserModel> findAll(UserModel userModel)
    {
        List<String> hobbyList = new ArrayList<>();
        if (!"".equals(userModel.getHobby()) && userModel.getHobby() != null)
        {
            String hobby = userModel.getHobby();
            String[] hbs = null;
            if (hobby != null && !"".equals(hobby))
            {
                hobby.split(",");
            }
            Collections.addAll(hobbyList, hbs);
            userModel.setHobbyList(hobbyList);
        }

        UserDao userDao = new UserDaoImpl();

        return userDao.findAll(userModel);
    }

    @Override
    public int deleteById(int id)
    {
        UserDao userDao = new UserDaoImpl();

        int count = userDao.deleteById(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public UserModel findById(int id)
    {
        UserDao userDao = new UserDaoImpl();

        return userDao.findById(id);
    }

    @Override
    public int edit(UserModel userModel)
    {
        UserDao userDao = new UserDaoImpl();

        if ("".equals(userModel.getUsername()))
        {
            userModel.setUsername(null);
        }
        if ("".equals(userModel.getPassword()))
        {
            userModel.setPassword(null);
        }
        if ("".equals(userModel.getPhone()))
        {
            userModel.setPhone(null);
        }
        if ("".equals(userModel.getSex()))
        {
            userModel.setSex(null);
        }
        if ("".equals(userModel.getBirthday()))
        {
            userModel.setBirthday(null);
        }
        if ("".equals(userModel.getHobby()))
        {
            userModel.setHobby(null);
        }
        if ("".equals(userModel.getIntroduce()))
        {
            userModel.setIntroduce(null);
        }

        int count = userDao.edit(userModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public int getCount(UserModel userModel)
    {
        UserDao userDao = new UserDaoImpl();

        return userDao.getCount(userModel);
    }

}
